package com.webcash.CNBoard.Controller;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.webcash.CNBoard.Model.Department;
import com.webcash.CNBoard.Model.Employee;
import com.webcash.CNBoard.Model.Login;
import com.webcash.CNBoard.Model.PositionName;
import com.webcash.CNBoard.Service.DepartmentService;
import com.webcash.CNBoard.Service.EmployeeService;
import com.webcash.CNBoard.Service.PositionNameService;

@Controller
@RequestMapping("/myaccount")
@SessionAttributes("login")
public class EmployeeAccountController {
	@Autowired
	EmployeeService employeeService;

	@Autowired
	DepartmentService departmentService;

	@Autowired
	PositionNameService positionNameService;

	@RequestMapping
	public String action(@ModelAttribute Login login, Model model){
		Employee emp = employeeService.getEmployeeById(login.getId());
		List<Department> dept = departmentService.getDepartmentList();
		Department empdept = departmentService.getDepartmentByDeptNo(Integer
				.parseInt(emp.getDeptNo()));
		PositionName emppos = positionNameService.getPositionNamebyPositionNo(Integer.parseInt(emp.getPosition()));
		List<PositionName> positionName = positionNameService.getPositionNameList();
		model.addAttribute("emp", emp);
		model.addAttribute("dept", dept);
		model.addAttribute("empdept", empdept);
		model.addAttribute("emppos", emppos);
		model.addAttribute("positionName", positionName);
		return "myaccount/myAccountPage";
	}

	@RequestMapping("/modifiePages")
	public String modifiedAction(@ModelAttribute Login login,Model model){
		Employee emp = employeeService.getEmployeeById(login.getId());
		List<Department> dept = departmentService.getDepartmentList();
		Department empdept = departmentService.getDepartmentByDeptNo(Integer
				.parseInt(emp.getDeptNo()));
		PositionName emppos = positionNameService.getPositionNamebyPositionNo(Integer.parseInt(emp.getPosition()));
		List<PositionName> positionName = positionNameService.getPositionNameList();
		model.addAttribute("emp", emp);
		model.addAttribute("dept", dept);
		model.addAttribute("empdept", empdept);
		model.addAttribute("emppos", emppos);
		model.addAttribute("positionName", positionName);
		return "myaccount/myAccountModifiePage";
	}

	@RequestMapping("/myAccountInfo")
	public String Information(@ModelAttribute Login login,Model model){
		Employee emp = employeeService.getEmployeeById(login.getId());
		List<Department> dept = departmentService.getDepartmentList();
		Department empdept = departmentService.getDepartmentByDeptNo(Integer
				.parseInt(emp.getDeptNo()));
		List<PositionName> positionName = positionNameService.getPositionNameList();
		PositionName emppos = positionNameService.getPositionNamebyPositionNo(Integer.parseInt(emp.getPosition()));
		model.addAttribute("emp", emp);
		model.addAttribute("dept", dept);
		model.addAttribute("empdept", empdept);
		model.addAttribute("emppos", emppos);
		model.addAttribute("positionName", positionName);
		return "myaccount/myAccountInfo";
	}

	@RequestMapping("/modifieAction")
	@ResponseBody
	public String writeAction(@ModelAttribute Login login, @ModelAttribute Employee emp, Model model) throws IllegalStateException, IOException{
		String data = "fail"; //or success 로 구분해서 ajax 컨트롤 유효성 검사하고 title이나 article 비어있는지
		emp.setId(login.getId());
		employeeService.getUpdateAccountResult(emp);
		

		return data;
	}

	@RequestMapping("/fileUploadAction")
	public String writeAction( @ModelAttribute Login login, Model model, HttpServletRequest request, @ModelAttribute Employee emp) throws IOException{
		MultipartHttpServletRequest mul = (MultipartHttpServletRequest)request;
		MultipartFile file = mul.getFile("file");
		String imgPath = "resources/upload/myaccountupload/";
		String root = request.getSession().getServletContext().getRealPath(imgPath);
		File fileName = new File(root);
		String saveFileName = login.getId() +"."+ file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")+1, file.getOriginalFilename().length());
		if(!fileName.exists()){
			fileName.mkdir();
		}
		fileName = new File(root, saveFileName);
		file.transferTo(fileName);
		emp.setId(login.getId());
		emp.setImgPath(imgPath+saveFileName);
		employeeService.setUpdateAccountImgPath(emp);
		return "redirect:/myaccount";
	}
}


